package DB;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import Data.FileData;


/**
 * search for file in the files table in DB
 * @author Erez
 *
 */

public class SearchFile {
	
	public SearchFile() 
	{
		
	}
	
    /**
     * opens a session with the DB and search for the file with the filename
     * @param filename the file name to be searched
     * @return
     */
	public synchronized boolean searchInDB(String filename)
	{
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();

     Query query = session.createQuery("from FileData");
     @SuppressWarnings("unchecked")
     List<FileData> list = query.list();
     FileData file;
     for(int i=0;i<list.size();i++)
     {
      file =list.get(i);
      if(file.getName().equals(filename)==true)
      {
       session.flush();
       return true;
      }
     }
     session.flush();
     return false;
	}
}
